package com.linln.admin.app.repository;

import com.linln.admin.app.domain.AppVersion;
import com.linln.common.config.BaseRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

/**
 * @author 陶富成
 * @date 2021/07/09
 */
public interface VersionRepository extends BaseRepository<AppVersion, Long>, JpaSpecificationExecutor<AppVersion> {

    List<AppVersion> findByPublished(String published);

    @Query(nativeQuery = true, value = "select * from app_version where onprod=:onprod order by publishtime desc limit 1")
    AppVersion findLatestByOnprod(String onprod);

    int countByVersionAndStatusAndIdNot(String version, Byte status, Long versionId);

    @Query(nativeQuery = true, value = "select count(*) from app_version where demand_url=:demand_url and status=:status and published='0' and id <> :versionId")
    int countByDemandUrlAndStatusAndIdNot(@Param("demand_url") String demand_url, @Param("status") Byte status, @Param("versionId") Long versionId);

    @Modifying
    @Query("update #{#entityName} set published = '2'  where id in ?1 ")
    public Integer discardByIds(List<Long> id);

    @Query(nativeQuery = true, value = "SELECT * FROM app_version WHERE published='0' AND `status`=1 order by id desc")
    List<AppVersion> findAllDevVers();

}